import MoreView from "./moreView.vue";
import NodeView from "./nodeView.vue";
export default function (Node, Renderer) {
    return Node.create({
        name: "Material",
        group: "block",
        content: "block*",
        defining: true,
        addAttributes() {
            return {
                id: {
                    parseHTML: (dom) => dom.getAttribute("id"),
                    renderHTML: (attributes) => ({
                        id: attributes.id,
                    }),
                },
                label: {
                    default: "",
                    parseHTML: (dom) => dom.getAttribute("data-label"),
                    renderHTML: (attributes) => ({
                        "data-label": attributes.label,
                    }),
                },
                type: {
                    default: "table",
                    parseHTML: (dom) => dom.getAttribute("data-type"),
                    renderHTML: (attributes) => ({
                        "data-type": attributes.type,
                    }),
                },
                material: {
                    default: [],
                    parseHTML: (dom) => {
                        const data = dom.getAttribute("data-material");
                        return data ? JSON.parse(data) : [];
                    },
                    renderHTML: (attributes) => ({
                        "data-material": JSON.stringify(attributes.material),
                    }),
                },
            };
        },
        parseHTML() {
            return [
                {
                    tag: "div.c-material",
                    getAttrs: (dom) => ({
                        id: dom.getAttribute("id"),
                        label: dom.getAttribute("data-label"),
                        type: dom.getAttribute("data-type"),
                        material: JSON.parse(
                            dom.getAttribute("data-material") || "[]"
                        ),
                        // total: dom.getAttribute("data-total"),
                    }),
                },
            ];
        },
        renderHTML({ HTMLAttributes }) {
            return ["div", { ...HTMLAttributes, class: "c-material" }];
        },
        addNodeView() {
            return Renderer(NodeView);
        },
        handleDelete() {
            if (this.editor.isActive(this.name)) {
                this.editor.commands.deleteNode(this.name);
                return true;
            }
            return false;
        },
        addOptions() {
            return {
                more(attrs) {
                    return <MoreView {...attrs} />;
                },
            };
        },
    });
}
